package com.lh.mybatisuse.controller;

import com.lh.mybatisuse.model.InPutParam.ZoneInParam;
import com.lh.mybatisuse.model.InPutParam.ZoneInsertInParam;
import com.lh.mybatisuse.service.ZoneService;
import com.lh.mybatisuse.unit.GetPropertiesClass;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lh.model.ResultVO;
import lh.units.ResultStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * @author ：梁昊，后端工程师：梁昊
 * @create 2021-06-22 17:14
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/zoneController")
@Api(value = "行政区划粒子层", description = "常规方法")
public class ZoneController {
    @Autowired
    ZoneService zoneService;
    @Autowired
    GetPropertiesClass getPropertiesClass;

    /**
     * 根据级别查询不分页，方法ID：SE20210622163040163837704EDFEAC
     *
     * @param label      表inf_zone,字段名label:地区名称
     * @param pinyYin    表inf_zone,字段名pinyYin:拼音码
     * @param classIndex 表inf_zone,字段名classIndex:级别
     * @param paraId     表inf_zone,字段名paraId:父ID
     * @return 行政区划
     */
    @ApiOperation(value = "根据级别查询不分页[行政区划],方法ID：SE20210622163040163837704EDFEAC", notes = "/zoneController/V1/selectZone?label=label&pinyYin=pinyYin&classIndex=classIndex&paraId=paraId&auditSign=auditSign")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "label", value = "地区名称", dataType = "String")
            , @ApiImplicitParam(name = "pinyYin", value = "拼音码", dataType = "String")
            , @ApiImplicitParam(name = "classIndex", value = "级别", dataType = "String", required = true)
            , @ApiImplicitParam(name = "paraId", value = "父ID", dataType = "String")
    })
    @PostMapping("/V1/selectZone")
    public ResultVO selectZone(@RequestParam(value = "label", required = false) String label
            , @RequestParam(value = "pinyYin", required = false) String pinyYin
            , @RequestParam(value = "classIndex", defaultValue = " - 128") int classIndex
            , @RequestParam(value = "paraId", required = false) String paraId) throws UnsupportedEncodingException {
        if (label != null) {
            label = label.trim();
            if (label.getBytes("gbk").length > 255) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：255", "label"), ResultVO.class, null);
            }
        }
        if (pinyYin != null) {
            pinyYin = pinyYin.trim();
            if (pinyYin.getBytes("gbk").length > 255) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：255", "pinyYin"), ResultVO.class, null);
            }
        }
        if (paraId != null) {
            paraId = paraId.trim();
            if (paraId.getBytes("gbk").length > 100) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：100", "paraId"), ResultVO.class, null);
            }
        }

        ZoneInParam zoneInParam = new ZoneInParam();
        zoneInParam.setLabel(label);
        zoneInParam.setPinyYin(pinyYin);
        if (classIndex > -128)
            zoneInParam.setClassIndex(classIndex);
        zoneInParam.setParaId(paraId);

        return ResultStruct.success(zoneService.selectZone(zoneInParam));
    }


    /**
     * 增加一条记录，方法ID：IN202106221727054622EDE3520AAA7
     *
     * @param id 主键
     * @param label 地区名称
     * @param pinyYin 拼音码
     * @param classIndex 级别
     * @param paraId 父ID
     * @param auditSign 审核状态
     * @return 行政区划
     */
    @ApiOperation(value = "增加一条记录[行政区划],方法ID：IN202106221727054622EDE3520AAA7", notes = "/zoneController/V1/insertZoneAllInfo?id=id&label=label&pinyYin=pinyYin&classIndex=classIndex&paraId=paraId&auditSign=auditSign")
    @ApiImplicitParams({
             @ApiImplicitParam(name = "id", value = "主键, 长度：100", dataType = "String", required = true)
            , @ApiImplicitParam(name = "label", value = "地区名称, 长度：255", dataType = "String", required = true)
            , @ApiImplicitParam(name = "pinyYin", value = "拼音码, 长度：255", dataType = "String", required = true)
            , @ApiImplicitParam(name = "classIndex", value = "级别, 长度：3", dataType = "String", required = true)
            , @ApiImplicitParam(name = "paraId", value = "父ID, 长度：100", dataType = "String", required = true)
            , @ApiImplicitParam(name = "auditSign", value = "审核状态, 长度：3", dataType = "String", required = true)
    })
    @PostMapping("/V1/insertZoneAllInfo")
    public ResultVO insertZoneAllInfo(@RequestParam(value = "id") String id
            , @RequestParam(value = "label") String label
            , @RequestParam(value = "pinyYin") String pinyYin
            , @RequestParam(value = "classIndex") int classIndex
            , @RequestParam(value = "paraId") String paraId
            , @RequestParam(value = "auditSign", defaultValue = "1") int auditSign) throws UnsupportedEncodingException {
        if (label != null) {
            label = label.trim();
            if (label.getBytes("gbk").length > 255) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：255", "label"), ResultVO.class, null);
            }
        }
        if (pinyYin != null) {
            pinyYin = pinyYin.trim();
            if (pinyYin.getBytes("gbk").length > 255) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：255", "pinyYin"), ResultVO.class, null);
            }
        }
        if (paraId != null) {
            paraId = paraId.trim();
            if (paraId.getBytes("gbk").length > 100) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：100", "paraId"), ResultVO.class, null);
            }
        }

        ZoneInsertInParam zoneInsertInParam = new ZoneInsertInParam();
        String mainKey = id;//这里引用架包中的生成主键方法
        zoneInsertInParam.setId(mainKey);
        zoneInsertInParam.setLabel(label);
        zoneInsertInParam.setPinyYin(pinyYin);
        zoneInsertInParam.setClassIndex(classIndex);
        zoneInsertInParam.setParaId(paraId);
        zoneInsertInParam.setAuditSign(auditSign);
        int resultCount = zoneService.insertZoneAllInfo(zoneInsertInParam);
        if (resultCount > 0)
            return ResultStruct.success(resultCount);
        else if (resultCount == -1000)
            return ResultStruct.error("重复，不允许增加", ResultVO.class, int.class);
        else
            return ResultStruct.error("增加失败", ResultVO.class, int.class);
    }

    /**
     * 查询省与城市，方法ID：SE20210623120240780454ECBF32DE3
     *
     * @param provinceLabel 表inf_zone,原字段名label,现字段名provinceLabel:地区名称
     * @return 行政区划
     */
    @ApiOperation(value = "查询省与城市[行政区划],方法ID：SE20210623120240780454ECBF32DE3", notes = "/zoneController/V1/selectZoneAndCityByProId?label=label&classIndex=classIndex&auditSign=auditSign")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "provinceLabel", value = "地区名称", dataType = "String")
    })
    @PostMapping("/V1/selectZoneAndCityByProId")
    public ResultVO selectZoneAndCityByProId(@RequestParam(value = "provinceLabel", required = false) String provinceLabel) throws UnsupportedEncodingException {
        if (provinceLabel != null) {
            provinceLabel = provinceLabel.trim();
            if (provinceLabel.getBytes("gbk").length > 255) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：255", "provinceLabel"), ResultVO.class, null);
            }
        }


        return ResultStruct.success(zoneService.selectZoneAndCityByProId(provinceLabel));
    }


}